package com.app.service.impl;

import com.app.common.AreasTool;
import com.app.common.EducationTool;
import com.app.common.ExperienceTool;
import com.app.common.SalaryTool;
import com.app.mapper.CustomerMapper;
import com.app.mapper.JobIntentionMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.*;
import com.app.vo.CustomerListVo;
import com.app.vo.CustomerVo;
import com.app.vo.ExpertVo;
import com.app.vo.ValueAndNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description:用户实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    private ConfigService configService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CusWorkExperienceService cusWorkExperienceService;
    @Autowired
    CusEducationService cusEducationService;
    @Autowired
    LookMeService lookMeService;
    @Autowired
    PositionService positionService;
    @Autowired
    JobIntentionMapper jobIntentionMapper;

    @Override
    public int insert(CustomerModel cus) {
        customerMapper.insert(cus);
        return cus.getId();
    }

    /***
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public CustomerModel getCustomerById(int id) {

        return customerMapper.selectById(id);
    }

    @Override
    public void update(CustomerModel cus) {

        customerMapper.updateById(cus);
    }

    @Override
    public int getCountAll() {
        return customerMapper.selectCount(null);
    }

    @Override
    public int getModelCountByTime(String crtTime, String startTime, String endTime, Integer type) {
        QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper();
        if (type == 1) {
            queryWrapper.apply(" TO_DAYS(crt_time) >= TO_DAYS('" + startTime + "')");
            queryWrapper.apply(" TO_DAYS(crt_time) <= TO_DAYS('" + endTime + "')");
        } else {
            queryWrapper.apply("TO_DAYS(crt_time) > TO_DAYS('" + crtTime + "')");
        }

        Integer count = customerMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public int getModelCountByLoginTime(String startTime, String endTime) {
        {
            QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper();
            queryWrapper.apply(" TO_DAYS(login_time) >= TO_DAYS('" + startTime + "')");
            queryWrapper.apply(" TO_DAYS(login_time) <= TO_DAYS('" + endTime + "')");
            Integer count = customerMapper.selectCount(queryWrapper);
            return count;
        }
    }

    @Override
    public int getModelCountByNull() {
        QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper();
        queryWrapper.isNull("real_name");
        Integer count = customerMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public int getModelCountByNoPublish() {
        QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper();
        queryWrapper.apply("id not in(select rec_id from tb_job_publish)");
        Integer count = customerMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public Object getCustomer(String token, Integer id) throws ParseException {


        CustomerModel cus = null;
        UserModel user = null;
        UserModel user2 = null;
        if (id != null && !"0".equals(id) && !"undefined".equals(id) && id > 0) {
            cus = getCustomerById(Integer.valueOf(id));
        } else {
            String mobile = TokenUtils.nothingness(token);
            if (mobile == null) {
                return Result.error(405, "token不存在");

            }
            Map<String, Object> map = new HashMap<>();
            map.put("mobile", mobile);
            List<UserModel> userList = userMapper.selectByMap(map);
            if (userList != null && userList.size() > 0) {
                user = userList.get(0);
            }
            cus = getCustomerById(user.getCId());
        }
        CustomerVo customerVo = new CustomerVo();
        if (cus == null) {
            return Result.error(405, "用户不存在");

        } else {
            Map<String, Object> map2 = new HashMap<>();
            map2.put("c_id", cus.getId());
            List<UserModel> userList2 = userMapper.selectByMap(map2);
            if (userList2 != null && userList2.size() > 0) {
                user2 = userList2.get(0);
            }
            cus.setUser(user2);
            List<CusWorkExperienceModel> workExpList = cusWorkExperienceService.getWorkExpByCustomerId(cus.getId());
            if (workExpList != null && workExpList.size() > 0)//工作经验
            {
                Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(workExpList.get(0).getWorkTimeStart() + "-01").getTime());
                String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
                customerVo.setWorkTime("工作" + date + "年");
            } else {
                customerVo.setWorkTime("无工作经验");
            }

            if (cus.getBirthday() != null)//年龄
            {
                Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(cus.getBirthday() + "-01").getTime());
                String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
                customerVo.setBirthday(date + "岁");
            } else {
                customerVo.setBirthday("暂无年龄");
            }
            List<CusEducationModel> education = cusEducationService.getEducationByCustomerId(cus.getId());
            if (education != null && education.size() > 0)//学历
            {
                for (int i = 0; i < EducationTool.EDUCATION_VO_LIST.size(); i++) {
                    if (education.get(0).getEducation() == EducationTool.EDUCATION_VO_LIST.get(i).getId()) {
                        customerVo.setEducation(EducationTool.EDUCATION_VO_LIST.get(i).getData());
                    }
                }
            } else {
                customerVo.setEducation("暂无学历");
            }
            if (user != null) {
                if (user.getRId() == null) {
                    user.setRId(0);
                }
                LookMeModel lookMe = lookMeService.findRidAndPid(0, user.getRId(), 0, cus.getId(), 2, 2);//查询收藏状态
                if (lookMe != null) {
                    customerVo.setLookType(1);
                } else {
                    customerVo.setLookType(2);

                }
            }

        }
        customerVo.setName(cus.getRealName());
        customerVo.setRealName(protectedName(cus.getRealName()));
        //头像
        Map<String, Object> map3 = new HashMap<>();
        map3.put("c_id", cus.getId());
        UserModel userModel = userMapper.selectByMap(map3).get(0);
        customerVo.setFaceUrl(userModel.getFaceUrl());
        customerVo.setTelPhone(cus.getUser().getMobile());
        return customerVo;
    }

    @Override
    public Object getUserCustomer(String token, Integer id) throws ParseException {
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token不存在");

        }
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        List<UserModel> userList = userMapper.selectByMap(map);
        UserModel user = null;
        if (userList != null && userList.size() > 0) {
            user = userList.get(0);
        }
        CustomerModel cus = null;
        if (id != null && !"0".equals(id) && !"undefined".equals(id)) {
            cus = getCustomerById(Integer.valueOf(id));
        } else {
            cus = getCustomerById(user.getCId());
        }
        CustomerVo customerVo = new CustomerVo();
        if (StringTool.isEmpty(cus.getEml())) {
            customerVo.setEml("");
        } else {
            customerVo.setEml(cus.getEml());

        }
        customerVo.setSex(cus.getSex());
        if (StringTool.isEmpty(cus.getBirthday())) {
            customerVo.setBirthday("");
        } else {
            customerVo.setBirthday(cus.getBirthday());

        }
        if (StringTool.isEmpty(cus.getWeChat())) {
            customerVo.setWeChat("");
        } else {
            customerVo.setWeChat(cus.getWeChat());

        }
        customerVo.setRealName(cus.getRealName());
        customerVo.setFaceUrl(user.getFaceUrl());
        customerVo.setJobStatus(cus.getJobStatus());
        customerVo.setStreetId(cus.getStreetId());
        return customerVo;
    }

    @Override
    public Object getCusAndIntentionList(CustomerModel model) throws ParseException {

        if (model.getSalaryId() != null && model.getSalaryId() > 0) {
            String salaryMin = null;
            String salaryMax = null;
            for (int i = 0; i < SalaryTool.SALARY_VO_LIST.size(); i++) {

                if (model.getSalaryId() > 1 && model.getSalaryId() < 9) {
                    if (model.getSalaryId() == SalaryTool.SALARY_VO_LIST.get(i).getId()) {
                        salaryMin = StringUtils.substringBefore(SalaryTool.SALARY_VO_LIST.get(i).getData(), "-");
                        salaryMax = StringUtils.substringAfter(SalaryTool.SALARY_VO_LIST.get(i).getData(), "-");
                        break;
                    }
                }
                if (model.getSalaryId() == 1) {
                    salaryMax = "1000";
                    break;
                }
                if (model.getSalaryId() == 9) {
                    salaryMin = "25000";
                    break;
                }
            }
            if (salaryMin != null && salaryMin != "") {
                model.setSalaryMin(Integer.parseInt(salaryMin));
            }
            if (salaryMax != null && salaryMax != "") {
                model.setSalaryMax(Integer.parseInt(salaryMax));
            }

        }

        if (model.getExpId() != null && model.getExpId() > 0) {
            int expMin = 0;
            int expMax = 0;
            for (int i = 0; i < ExperienceTool.EXPERIENCE_VO_LIST.size(); i++) {
                if (model.getExpId() > 1 && model.getExpId() < 6) {
                    if (model.getExpId() == ExperienceTool.EXPERIENCE_VO_LIST.get(i).getId()) {
                        expMin = Integer.parseInt(StringUtils.substringBefore(ExperienceTool.EXPERIENCE_VO_LIST.get(i).getData(), "-"));
                        expMax = Integer.parseInt(StringUtils.substringAfter(ExperienceTool.EXPERIENCE_VO_LIST.get(i).getData(), "-"));
                    }


                }
                if (model.getExpId() == 1) {
                    expMin = 1;
                }
                if (model.getExpId() == 6) {
                    expMax = 10;
                }
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.YEAR, -expMin);
            Date start = c.getTime();
            String startDay = format.format(start);//前expMin年


            c.add(Calendar.YEAR, -expMax);
            Date end = c.getTime();
            String endDay = format.format(end);//前expMax年
            if (model.getExpId() != 1) {
                model.setWorkTimeStart(startDay);
            }
            if (model.getExpId() != 6) {

                model.setWorkTimeEnd(endDay);
            }

        }
        if (model.getSearchName() != null && !model.getSearchName().equals("")) {
            model.setPage(1);
            model.setLimit(99999);
            List<CustomerModel> customerModelList = customerMapper.getCusAndIntentionList(model);
            int count = customerMapper.getCusAndIntentionCount(model);
            if (customerModelList == null || customerModelList.size() == 0) {
                CustomerListVo list = new CustomerListVo();
                list.setCount(count);
                list.setList(customerModelList);
                return list;
            } else {
                if (customerModelList == null || customerModelList.size() == 0) {
                    return null;
                }
                for (int i = 0; i < customerModelList.size(); i++) {
                    //计算年龄
                    if (customerModelList.get(i).getBirthday() != null) {
                        Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(customerModelList.get(i).getBirthday() + "-01").getTime());
                        String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
                        customerModelList.get(i).setBirthday(date);
                    }

                    //判断性别
                    if (customerModelList.get(i).getSex() == 1) {
                        customerModelList.get(i).setSexText("男");
                    } else {
                        customerModelList.get(i).setSexText("女");
                    }
                    //获取学历文字
                    List<CusEducationModel> education = cusEducationService.getEducationByCustomerId(customerModelList.get(i).getId());
                    if (education == null) {
                        customerModelList.get(i).setEducationText("暂无学历");
                    } else {
                        for (int k = 0; k < EducationTool.EDUCATION_VO_LIST.size(); k++) {
                            if (education.get(0).getEducation() == EducationTool.EDUCATION_VO_LIST.get(k).getId()) {
                                customerModelList.get(i).setEducationText(EducationTool.EDUCATION_VO_LIST.get(k).getData());
                            }
                        }
                        if (customerModelList.get(i).getEducationText() == null || customerModelList.get(i).getEducationText() == "") {
                            customerModelList.get(i).setEducationText("暂无学历");
                        }
                    }

                    List<CusWorkExperienceModel> workExpList = cusWorkExperienceService.getWorkExpByCustomerId(customerModelList.get(i).getId());
                    if (workExpList != null) {
                        Timestamp time2 = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(workExpList.get(0).getWorkTimeStart() + "-01").getTime());
                        String date2 = DateUtil.yearCompare(time2, CommonHelp.getNowTimestamp());
                        customerModelList.get(i).setWorkTimeStart(date2 + "年");
                    } else {
                        customerModelList.get(i).setWorkTimeStart("无");
                    }
                    if (customerModelList.get(i).getJobStatus() == 1) {
                        customerModelList.get(i).setJobText("离职-随时到岗");
                    } else if (customerModelList.get(i).getJobStatus() == 2) {
                        customerModelList.get(i).setJobText("在职-月内到岗");
                    } else if (customerModelList.get(i).getJobStatus() == 3) {
                        customerModelList.get(i).setJobText("在职-考虑机会");
                    } else if (customerModelList.get(i).getJobStatus() == 4) {
                        customerModelList.get(i).setJobText("在职-暂不考虑");
                    } else {
                        customerModelList.get(i).setJobText("暂无状态");
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put("c_id", customerModelList.get(i).getId());
                    List<UserModel> userList = userMapper.selectByMap(map);
                    UserModel user = null;
                    if (userList != null && userList.size() > 0) {
                        user = userList.get(0);
                    }
                    customerModelList.get(i).setUser(user);
                    PositionModel post = positionService.getModelById(Integer.valueOf(customerModelList.get(i).getPost()));
                    customerModelList.get(i).setPostName(post.getJobTitle());
                }

                if (customerModelList != null && customerModelList.size() > 0) {
                    for (int i = 0; i < customerModelList.size(); i++) {

                        for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {
                            if (Integer.valueOf(customerModelList.get(i).getRegProvince()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                                customerModelList.get(i).setRegProvince(AreasTool.AREA_VO_LIST.get(j).getData());
                                break;
                            }

                        }
                        for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {

                            if (Integer.valueOf(customerModelList.get(i).getRegCity()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                                customerModelList.get(i).setRegCity(AreasTool.AREA_VO_LIST.get(j).getData());
                                break;
                            }

                        }
                        for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {
                            if (Integer.valueOf(customerModelList.get(i).getRegArea()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                                customerModelList.get(i).setRegArea(AreasTool.AREA_VO_LIST.get(j).getData());
                                break;
                            }
                        }
                        if (StringTool.isNotEmpty(customerModelList.get(i).getLoginTime())) {

                            int ss = DateUtil.calLastedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(customerModelList.get(i).getLoginTime()));
                            if (ss < 86400)//一天
                            {
                                customerModelList.get(i).setLoginTimeText("1天内活跃");
                            } else if (ss > 86400 && ss < 259200) {
                                customerModelList.get(i).setLoginTimeText("3天内活跃");
                            } else if (ss > 259200 && ss < 604800) {
                                customerModelList.get(i).setLoginTimeText("7天内活跃");
                            } else if (ss > 604800 && ss < 2592000) {
                                customerModelList.get(i).setLoginTimeText("1月内活跃");
                            } else if (ss > 2592000 && ss < 7776000) {
                                customerModelList.get(i).setLoginTimeText("3月内活跃");
                            } else if (ss > 7776000 && ss < 15552000) {
                                customerModelList.get(i).setLoginTimeText("半年内活跃");
                            } else if (ss > 15552000 && ss < 31536000) {
                                customerModelList.get(i).setLoginTimeText("1年内活跃");
                            } else {
                                customerModelList.get(i).setLoginTimeText("1年内未活跃");
                            }
                        } else {
                            customerModelList.get(i).setLoginTimeText("7天内活跃");
                        }
                    }
                }
            }
            List<CustomerModel> collect = customerModelList.stream()
                    .filter(CustomerModel -> CustomerModel.getPostName().contains(model.getSearchName()))
                    .collect(Collectors.toList());
            CustomerListVo list = new CustomerListVo();
            list.setList(collect);
            list.setCount(collect.size());
            return list;
        }
        List<CustomerModel> customerModelList = customerMapper.getCusAndIntentionList(model);
        int count = customerMapper.getCusAndIntentionCount(model);

        if (customerModelList == null || customerModelList.size() == 0) {
            CustomerListVo list = new CustomerListVo();
            list.setCount(count);
            list.setList(customerModelList);
            return list;
        } else {
            if (customerModelList == null || customerModelList.size() == 0) {
                return null;
            }
            for (int i = 0; i < customerModelList.size(); i++) {
                //姓名脱敏
                String realName = protectedName(customerModelList.get(i).getRealName());
                customerModelList.get(i).setRealName(realName);
                //计算年龄
                if (customerModelList.get(i).getBirthday() != null) {
                    Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(customerModelList.get(i).getBirthday() + "-01").getTime());
                    String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
                    customerModelList.get(i).setBirthday(date);
                }

                //判断性别
                if (customerModelList.get(i).getSex() == 1) {
                    customerModelList.get(i).setSexText("男");
                } else {
                    customerModelList.get(i).setSexText("女");
                }
                //获取学历文字
                List<CusEducationModel> education = cusEducationService.getEducationByCustomerId(customerModelList.get(i).getId());
                if (education == null) {
                    customerModelList.get(i).setEducationText("暂无学历");
                } else {
                    for (int k = 0; k < EducationTool.EDUCATION_VO_LIST.size(); k++) {
                        if (education.get(0).getEducation() == EducationTool.EDUCATION_VO_LIST.get(k).getId()) {
                            customerModelList.get(i).setEducationText(EducationTool.EDUCATION_VO_LIST.get(k).getData());
                        }
                    }
                    if (customerModelList.get(i).getEducationText() == null || customerModelList.get(i).getEducationText() == "") {
                        customerModelList.get(i).setEducationText("暂无学历");
                    }
                }

                List<CusWorkExperienceModel> workExpList = cusWorkExperienceService.getWorkExpByCustomerId(customerModelList.get(i).getId());
                if (workExpList != null) {
                    Timestamp time2 = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(workExpList.get(0).getWorkTimeStart() + "-01").getTime());
                    String date2 = DateUtil.yearCompare(time2, CommonHelp.getNowTimestamp());
                    customerModelList.get(i).setWorkTimeStart(date2 + "年");
                } else {
                    customerModelList.get(i).setWorkTimeStart("无");
                }
                if (customerModelList.get(i).getJobStatus() == 1) {
                    customerModelList.get(i).setJobText("离职-随时到岗");
                } else if (customerModelList.get(i).getJobStatus() == 2) {
                    customerModelList.get(i).setJobText("在职-月内到岗");
                } else if (customerModelList.get(i).getJobStatus() == 3) {
                    customerModelList.get(i).setJobText("在职-考虑机会");
                } else if (customerModelList.get(i).getJobStatus() == 4) {
                    customerModelList.get(i).setJobText("在职-暂不考虑");
                } else {
                    customerModelList.get(i).setJobText("暂无状态");
                }
                Map<String, Object> map = new HashMap<>();
                map.put("c_id", customerModelList.get(i).getId());
                List<UserModel> userList = userMapper.selectByMap(map);
                UserModel user = null;
                if (userList != null && userList.size() > 0) {
                    user = userList.get(0);
                }
                customerModelList.get(i).setUser(user);
                PositionModel post = positionService.getModelById(Integer.valueOf(customerModelList.get(i).getPost()));
                customerModelList.get(i).setPostName(post.getJobTitle());
            }

            if (customerModelList != null && customerModelList.size() > 0) {
                for (int i = 0; i < customerModelList.size(); i++) {

                    for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {
                        if (Integer.valueOf(customerModelList.get(i).getRegProvince()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                            customerModelList.get(i).setRegProvince(AreasTool.AREA_VO_LIST.get(j).getData());
                            break;
                        }

                    }
                    for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {

                        if (Integer.valueOf(customerModelList.get(i).getRegCity()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                            customerModelList.get(i).setRegCity(AreasTool.AREA_VO_LIST.get(j).getData());
                            break;
                        }

                    }
                    for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {
                        if (Integer.valueOf(customerModelList.get(i).getRegArea()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                            customerModelList.get(i).setRegArea(AreasTool.AREA_VO_LIST.get(j).getData());
                            break;
                        }
                    }
                    if (StringTool.isNotEmpty(customerModelList.get(i).getLoginTime())) {

                        int ss = DateUtil.calLastedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(customerModelList.get(i).getLoginTime()));
                        if (ss < 86400)//一天
                        {
                            customerModelList.get(i).setLoginTimeText("1天内活跃");
                        } else if (ss > 86400 && ss < 259200) {
                            customerModelList.get(i).setLoginTimeText("3天内活跃");
                        } else if (ss > 259200 && ss < 604800) {
                            customerModelList.get(i).setLoginTimeText("7天内活跃");
                        } else if (ss > 604800 && ss < 2592000) {
                            customerModelList.get(i).setLoginTimeText("1月内活跃");
                        } else if (ss > 2592000 && ss < 7776000) {
                            customerModelList.get(i).setLoginTimeText("3月内活跃");
                        } else if (ss > 7776000 && ss < 15552000) {
                            customerModelList.get(i).setLoginTimeText("半年内活跃");
                        } else if (ss > 15552000 && ss < 31536000) {
                            customerModelList.get(i).setLoginTimeText("1年内活跃");
                        } else {
                            customerModelList.get(i).setLoginTimeText("1年内未活跃");
                        }
                    } else {
                        customerModelList.get(i).setLoginTimeText("7天内活跃");
                    }
                }
            }
        }
        CustomerListVo list = new CustomerListVo();
        list.setCount(count);
        list.setList(customerModelList);
        return list;
    }

    @Override
    public Integer selectSeekersCount() {
        return customerMapper.selectCount(null);
    }

    @Override
    public int[] agePie() throws ParseException {
        List<CustomerModel> customerModels = customerMapper.selectList(null);
        List<ValueAndNameVo> vos = new ArrayList<>();
        for (int i = 0; i < customerModels.size(); i++) {
            if (customerModels.get(i).getBirthday() != null)//年龄
            {
                Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(customerModels.get(i).getBirthday() + "-01").getTime());
                String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
                customerModels.get(i).setAge(Integer.valueOf(date));
            } else {
                customerModels.get(i).setAge(0);
            }
        }
        //16-25  25-35 35-45 45-55
        List<CustomerModel> collect = customerModels.stream()
                .filter(CustomerModel -> CustomerModel.getAge() >= 16 && CustomerModel.getAge() < 25)
                .collect(Collectors.toList());
        List<CustomerModel> collect1 = customerModels.stream()
                .filter(CustomerModel -> CustomerModel.getAge() >= 25 && CustomerModel.getAge() < 35)
                .collect(Collectors.toList());
        List<CustomerModel> collect2 = customerModels.stream()
                .filter(CustomerModel -> CustomerModel.getAge() >= 35 && CustomerModel.getAge() < 45)
                .collect(Collectors.toList());
        List<CustomerModel> collect3 = customerModels.stream()
                .filter(CustomerModel -> CustomerModel.getAge() >= 45 && CustomerModel.getAge() < 55)
                .collect(Collectors.toList());
        List<CustomerModel> collect4 = customerModels.stream()
                .filter(CustomerModel -> CustomerModel.getAge() > 55)
                .collect(Collectors.toList());//55以上
        int[] arr = {collect.size(), collect1.size(), collect2.size(), collect3.size(), collect4.size()};
        return arr;
    }

    @Override
    public void customerLoginMak(CustomerModel model) {
        model.setLoginTime(CommonHelp.getNowTimestamp().toString());
        customerMapper.updateById(model);
    }

    @Override
    public List<CustomerModel> getCustomerListAll() {

        return customerMapper.selectList(null);
    }

    @Override
    public int[] selectSexCount() {
        QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sex", 1);
        Integer male = customerMapper.selectCount(queryWrapper);
        QueryWrapper<CustomerModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("sex", 2);
        Integer female = customerMapper.selectCount(queryWrapper1);
        int[] arr = {male, female};
        return arr;
    }

    @Override
    public List<CustomerModel> selectAll(Integer page, Integer limit, Integer streetId) {
        page = (page - 1) * limit;
        List<CustomerModel> customerModels = customerMapper.selectAll(page, limit, streetId);
        List<JobIntentionModel> jobIntentionModels = jobIntentionMapper.selectList(null);
        for (int i = 0; i < customerModels.size(); i++) {
            //查询是否完善并返回资料完善状态字段
            int id = customerModels.get(i).getId();
            QueryWrapper<JobIntentionModel> qw = new QueryWrapper<>();
            qw.eq("customer_id", id);
            List<JobIntentionModel> jobIntentionList = jobIntentionMapper.selectList(qw);
            if (CollectionUtils.isNotEmpty(jobIntentionList)) {
                customerModels.get(i).setDatePerfectStatusText("资料已完善");
            } else {
                customerModels.get(i).setDatePerfectStatusText("资料未完善");
            }

            for (int i1 = 0; i1 < jobIntentionModels.size(); i1++) {
                if (jobIntentionModels.get(i1).getCustomerId() != null && customerModels.get(i).getId() == jobIntentionModels.get(i1).getCustomerId()) {
                    customerModels.get(i).setJobIntentionModels(jobIntentionModels.get(i1));
                }
            }
        }
        return customerModels;
    }

    @Override
    public int selectAllCount(Integer streetId) {
        return customerMapper.selectAllCount(streetId);
    }

    @Override
    public List<CustomerModel> getCustomerByStreet(String startTime, String endTime) {
        return customerMapper.getCustomerByStreet(startTime, endTime);
    }

    @Override
    public List<CustomerModel> getCustomerAndStreet(String startTime, String endTime) {
        List<CustomerModel> list = customerMapper.getCustomerAndStreet(startTime, endTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getCrtTime() != null) {
                list.get(i).setCrtTimeText(sdf.format(list.get(i).getCrtTime()));
            }

        }
        return list;
    }

    @Override
    public CustomerListVo getCustomerPageByStreetId(CustomerModel model) {
        QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper<>();
        if (model.getStreetId() != null && model.getStreetId() > 0) {
            queryWrapper.eq("street_id", model.getStreetId());
        }
        if (StringTool.isNotEmpty(model.getStartTime())) {
            queryWrapper.apply(" TO_DAYS(crt_time) >= TO_DAYS('" + model.getStartTime() + "')");
        }
        if (StringTool.isNotEmpty(model.getEndTime())) {
            queryWrapper.apply(" TO_DAYS(crt_time) <= TO_DAYS('" + model.getEndTime() + "')");
        }
        Page<CustomerModel> pages = new Page<>();
        pages.setCurrent(model.getPage());
        pages.setSize(model.getLimit());
        Page<CustomerModel> page = customerMapper.selectPage(pages, queryWrapper);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < page.getRecords().size(); i++) {
            if (page.getRecords().get(i).getCrtTime() != null) {
                page.getRecords().get(i).setCrtTimeText(sdf.format(page.getRecords().get(i).getCrtTime()));
            }

        }
        if (page == null) {
            return null;
        } else {
            CustomerListVo vo = new CustomerListVo();
            vo.setList(page.getRecords());
            vo.setCount((int) page.getTotal());
            return vo;
        }
    }

    @Override
    public List<Integer> selectTrendOfVisits() throws ParseException {
        Date date = new Date();
        //将时间格式化成yyyy-MM-dd HH:mm:ss的格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //创建Calendar实例
        Calendar cal = Calendar.getInstance();
        //设置当前时间
        cal.setTime(date);
        //获取前一天时间
        cal.add(Calendar.DATE, -1);
        String s = format.format(cal.getTime());
        String[] split = s.split(" ");
        String startTime = split[0] + " 00:00:00";
        String endTime = split[0] + " 23:59:59";
        List<CustomerModel> customerModels = customerMapper.selectTrendOfVisits(startTime, endTime);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            int count = 0;
            for (int i1 = 0; i1 < customerModels.size(); i1++) {
                if (StringTool.isNotEmpty(customerModels.get(i1).getLoginTime())) {
                    Date date1 = format.parse(customerModels.get(i1).getLoginTime());
                    if (date1.getHours() == i) {
                        count++;
                    }
                }
            }
            list.add(count);
        }
        return list;
    }
    /**
     * reg peo镇街
     *
     * @return {@link Map}<{@link String},{@link Object}>
     */
    @Override
    public List<Map<String, Object>> regPeoTownStreet() {
        List<Map<String, Object>> regPeoTownStreetCount = this.customerMapper.regPeoTownStreetCount();
        List<Map<String, Object>> responseMap = new ArrayList<>();
        List<String> staticsTown = this.configService.getStaticsTown();

        for (String s : staticsTown
        ) {
            Map<String, Object> town = new HashMap<>();
            town.put("name", s);
            regPeoTownStreetCount.stream().forEach(
                    m -> {
                        if (m.get("name").equals(s)) {
                            town.put("count", m.get("count"));
                        }
                    });
            if (!town.containsKey("count")) {
                town.put("count", 0);
            }
            responseMap.add(town);
        }
        return responseMap;
    }


    @Override
    public List<Map<String, Object>> browserPeoTownStreet() {
        List<Map<String, Object>> regPeoTownStreetCount = this.customerMapper.browserPeoTownStreetCount();
        List<Map<String, Object>> responseMap = new ArrayList<>();
        List<String> staticsTown = this.configService.getStaticsTown();

        for (String s : staticsTown
        ) {
            Map<String, Object> town = new HashMap<>();
            town.put("name", s);
            regPeoTownStreetCount.stream().forEach(
                    m -> {
                        if (m.get("name").equals(s)) {
                            town.put("count", m.get("count"));
                        }
                    });
            if (!town.containsKey("count")) {
                town.put("count", 0);
            }
            responseMap.add(town);
        }
        return responseMap;
    }

    /**
     * 姓脱敏
     *
     * @param userName
     * @return
     */
    public static String protectedName(String userName) {
        userName = userName.trim();
        char[] r = userName.toCharArray();
        String resultName = "";
        if (r.length == 2) {
            resultName = r[0] + "*";
        }
        if (r.length > 2) {
            String star = "";
            for (int i = 0; i < r.length - 2; i++) {
                star = star + "*";
            }
            resultName = r[0] + star + r[r.length - 1];
        }
        return resultName;
    }


}
